const vscode = require('vscode');

module.exports = function (context) {

  let disposable = vscode.commands.registerCommand('extension.consoleLogger', function () {
    // The code you place here will be executed every time your command is executed

    // 步骤分解
    // 1. 获取选中文本信息
    // 2. 获取选中文本位置
    // 3. 获取console分隔符配置
    // 4. 添加console代码

    const activeTextEditor = vscode.window.activeTextEditor;
    const activeDocument = activeTextEditor.document;
    const fileName = activeDocument.fileName;
    const fileSplitArr = fileName.split("\\");


    // 1. 获取所有选中行信息
    const selection = activeTextEditor.selection;
    // sample for selection: {"start":{"line":2,"character":0},"end":{"line":2,"character":7},"active":{"line":2,"character":7},"anchor":{"line":2,"character":0}}
    const { start, end } = selection;

    // 当前行文本内容
    const curLineText = activeDocument.lineAt(start.line).text;

    // 当前行非空文本起始位置
    const curLineStartCharacter = curLineText.search(/\S/i);

    // 当前行为空文本
    const curBlankText = curLineText.substring(0, curLineStartCharacter);

    // 当前选中文本内容
    const curText = curLineText.substring(start.character, end.character);

    // console插入位置
    const insertPositon = new vscode.Position(start.line + 1, 0);

    // 获取配置
    const config = vscode.workspace.getConfiguration('vscodePluginConsoleLogger');
    const identifier = config.get('identifier');

    // 调用编辑接口
    activeTextEditor.edit((TextEditorEdit) => {
      TextEditorEdit.insert(insertPositon, `${curBlankText}console.log('%c第${start.line + 1}行 ${curText}：','color:green', ${curText} )\n`);
    });
  });

  context.subscriptions.push(disposable);

};

